<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/26
 * Time: 10:50
 */

namespace Api\Controller;


use Common\Api\AccountApi;
use Common\ConstVar\BoyeActionConstModel;
use Uclient\Model\OAuth2TypeModel;
use Uclient\Model\UcenterMemberModel;

class SinaWeiboController extends ApiController {

    private $appid;
    private $appsecret;

    public function _init(){
        parent::_init();
        //新浪开放应用的appid和appsecret
        $this->appid = "2334687309";
        $this->appsecret = "1a7cc97ee01f4a787a8a29b6da09e36b";
        vendor('saetv2',APP_PATH.'Api/Vendor/SinaWeibo/','.ex.class.php');
    }

    /**
     * 调用新浪微博登录接口
     */
    public function login(){

        // 用户的OpenID/OpenKey
        $openid = $this->_post('openid','','缺少openid');
        $sina_token = $this->_post('sina_token','','缺少微博的sina_token');

        //1. 检测是否注册过
        $result = apiCall(AccountApi::GET_INFO_BY_SINA_WEIBO_OPENID,array($openid));

        addLog("SinaWeibo/login",$result,$result,"新浪登录从数据库获取的用户信息".$openid);
        //如果已经存在，则返回信息
        if($result['status'] && is_array($result['info'])){
            $this->apiReturnSuc($result['info']);
        }elseif($result['status'] === false){
            $this->apiReturnErr($result['info']);
        }

        $result = $this->get_user_info($sina_token,$openid);
        addLog("SinaWeibo/login",$result,$result,"新浪登录从新浪拉取的用户信息".$openid);
        if(!$result['status']){
            $this->apiReturnErr($result['info']);
        }
        $info = $result['info'];
        $icon_url = $info['avatar_large'];
        $nickname = $info['screen_name'];
        $sex = $info['gender'];
        $password = md5($openid);
        $idcode = getIDCode(rand(10000000000,99999999999),'Q');

        $entity = array(
            'type'=> UcenterMemberModel::ACCOUNT_TYPE_USERNAME,
            'username' => 'sina_'.$openid,
            'password' => $password,
            'from' => OAuth2TypeModel::SINA,
            'mobile' => '',
            'realname' => '',
            'nickname' => $nickname ,
            'email' => '',
            'idnumber' => '',
            'birthday' => time(),
            'idcode'=>$idcode,
            'head'=>$icon_url,
            'sina_openid'=>$openid,
            'sex'=>$sex,
        );

        $result = apiCall(AccountApi::REGISTER, array($entity));

        addLog("SinaWeibo/login",$result,$result,"新浪微博登录-注册");
        if($result['status']){
            $uid = $result['info'];
            if($uid > 0){
                $result = apiCall(AccountApi::GET_INFO,array($uid));

                if($result['status']){
                    action_log(BoyeActionConstModel::API_USER_LOGIN, "member", $uid, $uid);
                    $this->apiReturnSuc($result['info']);
                }
            }
        }

        $this->apiReturnErr($result['info']);




    }

    /**
     * 获取资料
     * @param $access_token
     * @param $uid
     * @return array
     */
    private function get_user_info($access_token,$uid)
    {
        $sina = new \SaeTClientV2( $this->appid  , $this->appsecret , $access_token, NULL);

        $result = $sina->show_user_by_id($uid);
        if(isset($result['error_code'])){
            return array('status'=>false,'info'=>$result['error_code'].'=>'.$result['error']);
        }
        return array('status'=>true,'info'=>$result);
    }



}